



/*

Table 4

*/



local nametable = "tables/rev_cropyieldsperhacultivated" 



forvalues xd=1(1)1{
	
	if `xd'==1{
		local xdist = 50
	}
	if `xd'==2{
		local xdist = 25
	}
	if `xd'==3{
		local xdist = 100
	}


	*-----
	eststo clear
	local column_number = 1
	local column_name = "column" + "`column_number'"
	*---	
	
	


	use "public_data/g30.dta", clear
	local xindex = 0
	foreach var of varlist cassavayam_prdnt cocoacoffeetea_prdnt cotton_prdnt groundnut_prdnt maize_prdnt millet_prdnt wspotato_prdnt pulses_prdnt rice_prdnt sorghum_prdnt{

	local xindex = `xindex'+1
	di "`xindex'"

	use "public_data/g30.dta", clear
	drop if `var'==.
	gen inter = index("`var'","_")
	local crop = substr("`var'",1,inter-1)
	local areac = "`crop'"+"_areat"
	local crop1 = proper("`crop'")
	local crop2 = " "
	local crop3 = " "
	drop inter
	if "`var'"=="sugarcane_prdnt"{
		local crop1 = "Sugar"
		local crop2 = "cane"
	}
	if "`var'"=="sugarbeet_prdnt"{
		local crop1 = "Sugar"
		local crop2 = "beet"
	}
	if "`var'"=="wspotato_prdnt"{
		local crop1 = "Potato,"
		local crop2 = "sweet"
		local crop3 = "potato"
	}
	if "`var'"=="cassavayam_prdnt"{
		local crop1 = "Cassava,"
		local crop2 = "yam"
	}
	if "`var'"=="bancocon_prdnt"{
		local crop1 = "Banana,"
		local crop2 = "coconut"
	}
	if "`var'"=="oilpalm_prdnt"{
		local crop1 = "Palm oil"
	}
	if "`var'"=="cerealoth_prdnt"{
		local crop1 = "Other"
		local crop2 = "cereals"
	}
	if "`var'"=="cocoacoffeetea_prdnt"{
		local crop1 = "Cocoa,"
		local crop2 = "coffee,"
		local crop3 = "tea"
	}
	if "`var'"=="vegetables_prdnt"{
		local crop1 = "Vegetables"
	}

	gen interdep = `var'/`areac'

	replace weight = 1

	
	*computes mean of dependent var
	capture drop interm
	egen interm = mean(interdep)
	local vmeandep = interm[1]
	local meandep = string(`vmeandep')
	if index("`meandep'",".")==1{	
		local meandep = "0"+substr("`meandep'",1,3)
	}
	if index("`meandep'",".")<5&index("`meandep'",".")>1{
		local meandep = substr("`meandep'",1,4)
	}

	*computes the number of country pairs
	egen intercp = group(border)
	egen intercpmax = max(intercp)
	local countrypair = intercpmax[1]
	drop intercp*

*	xi: areg interdep rdist1* varofi  [aw=weight] ,a(groupfe) cluster(border)
	reghdfe interdep rdist1*  varofi [aw=weight], absorb(groupfe) vce(cluster groupfe country)
	eststo `column_name'
	estadd local space  					" "
	estadd local maxdist 					"`xdist'"
	estadd local countrypair 					"`countrypair'"
	estadd local polynomial				"Linear"
	estadd local groupfe				"Yes"
	estadd local crop1						"`crop1'"
	estadd local crop2						"`crop2'"
	estadd local crop3						"`crop3'"
	estadd local nmeandep				"`meandep'"
	local column_number = `column_number' + 1
	local column_name = "column" + "`column_number'"
	

	
}	

label var interdep  				" "		
local nametable1 = "`nametable'" + "`xd'" + ".tex"

local depvar= "\textit{Dep var: Crop yield per hectare}"

#delimit ;
esttab column1 column2 column3 column4 column5 column6 column7 column8 column9 column10
using "`nametable1'", replace
label
b(3)
se(3)
star(* 0.10 ** 0.05 *** 0.01)
width(1.0\hsize)
nonotes
nolines
noconstant
mgroups("`depvar'", pattern(1 0 0 0 0 0 0 0 0)
prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) 
keep(varofi)
order(varofi)
  stats(groupfe					space	polynomial			space	r2		N					countrypair				space	maxdist							space	crop1		crop2	crop3	space	nmeandep,
    fmt(0						0		0					0		3		0					0					0		0								0		0			0		0		0		0)
layout(@						@		@					@		@		@					@					@		@								@		@			@		@		@		@)	 	
labels("Border x Ethnic FE"		" "		"RD polynomial"		" "		"R2"	"\# Observations" 	"\# Country pairs"	" "		"Max distance to border (km)"	" "		"Crop"		" "		" "		" "		"Mean Dep. Var.")
 )	
noobs
booktabs
;
#delimit cr


}
	



